<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<#include "/common/header.ftl">
    <title></title>
</head>
<body>
	<div title="新建投资活动">
		<form id="addInvestActivityForm" enctype="multipart/form-data" method="post"> 
	        <fieldset style="margin-right: 800px;" >
	        <input type="hidden" id="publishPlatforms" name="publishPlatforms" value=""/> 
		    <legend>标准信息</legend>
		         <table class="formTable" id="add_activity_Tab">
	                 <tr>
	                     <td class="label"> 标题：</td>
	                     <td class="value" >
				        	<input name="rewardPoint" id="rewardPoint" type="hidden" value="20"/>
				        	<input name="investType" id="investType" type="hidden" value="1"/>
	                        <input name="title" id="title" data-options="required:true" class="easyui-validatebox" onblur="searchActTitle();"/>
	                     </td>
	                 </tr>
	                 <tr>
		       		  	<td class="label">发布平台：</td>
		       			<td class="value" colspan="3">
		       				<input id="publishPlatforms" type="hidden" name="publishPlatforms"/>
		       				<input id="publishPlatforms1" value="1" type="checkbox" class="easyui-validatebox" />PC
		       				<input id="publishPlatforms2" value="2" type="checkbox" class="easyui-validatebox" />APP
		       				<input id="publishPlatforms3" value="3" type="checkbox" class="easyui-validatebox" />触屏版
		       			</td>
		       		</tr>
		       		 <tr>
		       		  	<td class="label">可投标类型：</td>
		       			<td class="value" colspan="3">
		       				<input id="borrowTypes" type="hidden" name="borrowTypes"/>
		       				<input id="borrowTypes1" value="3" type="checkbox" class="easyui-validatebox"/>新手标
		       				<input id="borrowTypes2" value="1" type="checkbox" class="easyui-validatebox"/>活期宝
		       				<input id="borrowTypes3" value="2" type="checkbox" class="easyui-validatebox"/>优选计划
		       				<input id="borrowTypes4" value="0" type="checkbox" class="easyui-validatebox"/>散标
		       				<input id="borrowTypes5" value="5" type="checkbox" class="easyui-validatebox"/>游艇
		       				<input id="borrowTypes6" value="6" type="checkbox" class="easyui-validatebox"/>酒店
		       				<input id="borrowTypes8" value="8" type="checkbox" class="easyui-validatebox"/>秒借
		       				<input id="borrowTypes9" value="9" type="checkbox" class="easyui-validatebox"/>有用分期
		       				<input id="borrowTypes10" value="10" type="checkbox" class="easyui-validatebox"/>车信宝
		       				<input id="borrowTypes99" value="99" type="checkbox" class="easyui-validatebox"/>集合包
		       				<!-- <input id="borrowTypes7" value="4" type="checkbox" class="easyui-validatebox"/>专享宝 -->
		       			</td>
		       		</tr>
	                 <tr>
			             <td class="label">可获奖励条件：</td>
			             <td class="value" >
			             	<input name = "rule" type="hidden" id = "rewardPreRule">
			             	<table  id="optionContainer">
			             		<!-- <tr>
			             			<td>
			             				<input name="rewardPreCon" id="rewardPreCon1" class="easyui-combobox" data-options="required:true" value=""/>
			             			</td>
			             			<td>
			             				<a onclick="addRow()" class="easyui-linkbutton"  iconCls="icon-add" ></a>
			             			</td>
			             		</tr> -->
			             	</table>
			             </td>
			         </tr>
	                 <tr>
			             <td class="label">投资金额：</td>
			             <td class="value" >
			             	<input name="investAmount" id="investAmount" class="easyui-numberbox" style="width: 240px" data-options="min:0.01,max:10000000,precision:2,required:true"
			             	value=""/>元
			             </td>
			         </tr>
	                 <tr>
	                     <td align="right"  class="label">活动开始时间：</td>
	                     <td class="value">
	                     	<input id = "registerStart" name="registerStart"  class="easyui-datetimebox" style="width: 180px" data-options="required:true" editable="false"/>
	                     </td>
	                 </tr>
	                 <tr>
	                     <td align="right" class="label">活动结束时间：</td>
	                     <td class="value">
	                     	<input name="registerEnd"  class="easyui-datetimebox" style="width: 180px" data-options="required:true" editable="false" validType="dataEqualTo['#registerStart']"/>
	                     </td>
	                 </tr>
	                 <tbody id="tickets"></tbody>
	                 <tr>
	                     <td align="right" class="label"> 活动介绍：</td>
	                     <td class="value">
	                        <textarea name="introduction" rows="8" cols="100"></textarea>
	                     </td>
	                 </tr>
	                 <tr>
	                     <td align="right" class="label"> 状态：</td>
	                     <td class="value">
	                     	<select name="status" class="easyui-combobox" style="width: 180px;"  
	                  			data-options="panelHeight:'auto',editable:false,required:true">
	                           <option value="1">生效</option>
	                           <option value="2">失效</option>
	                        </select>
	                     </td>
	                 </tr>
	           </table>         
	        </fieldset>
	        <fieldset style="margin-right: 800px;">
		    <legend>规则设置</legend>
		         <table class="formTable" id="table_rule">
		            <tbody id="tbo">
		        		<tr>
		        			<td class="label">投资月份</td>
		        			<td class="label">获取方式</td>
		        			<td class="label">投资区间(满投金额)</td>
		        			<td class="label">奖励</td>
		        			<td class="label">操作</td>
		        		</tr>
	        		</tbody>
	        		<tfoot>
	        		      <tr>
	        		          <td colspan="6">
	        		              <center>
	        		                   <a onclick="addRule()" class="easyui-linkbutton" iconCls="icon-save">添加设置</a> 
	        		              </center>
	        		          </td>
	        		      </tr>
	        		</tfoot>
	           </table>
	        </fieldset>

	        <fieldset style="margin-right: 800px;">
		    	<input id="cooperationIdsG" name="cooperationIds" type="hidden"/>
		         <table class="formTable">
	        		<tfoot>
	        		      <tr>
	        		          <td colspan="4">
	        		              <center>
	        		                   <a onclick="submit()"  class="easyui-linkbutton" iconCls="icon-save">保存</a>
	        		              </center>
	        		          </td>
	        		      </tr>
	        		</tfoot>
	           </table>
	        </fieldset>
	    </form>
	</div>
	
	<div id="add_rule_dialog" class="easyui-dialog" style="width:600px;height:250px;padding:10px 20px" closed="true"
     	buttons="#dlg-buttons"data-options="modal:true">
     	<input type="hidden" id="rule_flag" value=""/>
     	<input type="hidden" id="rule_count" value=""/>
     	<form id="add_rule_form" method="post">
           <table class="formTable" id="formTable_rule" >
                <tr>
		             <td class="label">投资月份：</td>
		             <td class="value">
		             	<input id="investMonth" class="easyui-numberbox" style="width: 240px" data-options="precision:0"
		             	value=""/>月
		             </td>
		         </tr>
		         <tr>
		         	<td class="label">获取方式:</td>
		         	<td class="value">
		         		<select name="investAmountCheckType" id="investAmountCheckType" class="easyui-combobox" style="width: 180px;" data-options="panelHeight:'auto',editable:false,required:true">
		         			<option value="1">按投资区间</option>
		         			<option value="2">按投资金额</option>
		         		</select>
		         	</td>
		         </tr>
		         <tr id="_investAmountFrom">
		             <td class="label" >投资下限：</td>
		             <td class="value">
		             	<input id="investAmountFrom" class="easyui-numberbox" style="width: 240px" data-options="precision:2"
		             	value=""/>元
		             </td>
		         </tr>
		         <tr id="_investAmountTo">
		             <td class="label">投资上限：</td>
		             <td class="value">
		             	<input id="investAmountTo" class="easyui-numberbox" style="width: 240px" data-options="precision:2"
		             	value=""/>元
		             </td>
		         </tr>
		         <tr style ="display:none;" id ="_investAmountDivisor">
		         	<td class="label">单笔每满：</td>
		         	<td class="value">
		         		<input name="investAmountDivisor" id="investAmountDivisor" class="easyui-numberbox" style="width:240px" data-options="precision:2,max:1000000,min:0.01"/>元
		         	</td>
		         </tr>
		         <tr>
		             <td class="label">奖池id：</td>
		             <td class="value">
		             	<input id="ticketId" class="easyui-combobox" style="width: 240px" data-options="editable:false,required:true"
		             	value=""/>
		             </td>
		         </tr>
		         <tr>
                     <td align="center" colspan="2">
                     	<a onclick="ruleSave()" class="easyui-linkbutton" iconcls="icon-ok">保存</a> 
                     	&nbsp;&nbsp;
         				<a onclick="ruleCancel()" class="easyui-linkbutton" iconcls="icon-cancel">取 消</a>
                     </td>
                 </tr>
           </table>
        </form>              
	</div>
	
	<script>
	$(function () {
		$("#borrowTypes2").change(function() { 		
			$("#borrowTypes1").attr("checked",false);
			$("#borrowTypes3").attr("checked",false);
			$("#borrowTypes4").attr("checked",false);
			$("#borrowTypes5").attr("checked",false);
			$("#borrowTypes6").attr("checked",false);
			$("#borrowTypes8").attr("checked",false);
			$("#borrowTypes7").attr("checked",false);
			$("#borrowTypes9").attr("checked",false);
			$("#borrowTypes10").attr("checked",false);
			$("#borrowTypes99").attr("checked",false);
			
			if($("#borrowTypes2").attr("checked")){	
				$("#borrowTypes1").attr("disabled",true);
				$("#borrowTypes3").attr("disabled",true);
				$("#borrowTypes4").attr("disabled",true);
				$("#borrowTypes5").attr("disabled",true);
				$("#borrowTypes6").attr("disabled",true);
				$("#borrowTypes8").attr("disabled",true);
				$("#borrowTypes7").attr("disabled",true);
				$("#borrowTypes9").attr("disabled",true);
				$("#borrowTypes10").attr("disabled",true);
				$("#borrowTypes99").attr("disabled",true);
			}else{
				$("#borrowTypes1").attr("disabled",false);
				$("#borrowTypes3").attr("disabled",false);
				$("#borrowTypes4").attr("disabled",false);
				$("#borrowTypes5").attr("disabled",false);
				$("#borrowTypes6").attr("disabled",false);
				$("#borrowTypes8").attr("disabled",false);
				$("#borrowTypes7").attr("disabled",false);
				$("#borrowTypes9").attr("disabled",false);
				$("#borrowTypes10").attr("disabled",false);
				$("#borrowTypes99").attr("disabled",false);
			}
		}); 
		
		$("#borrowTypes7").change(function() { 		
			$("#borrowTypes1").attr("checked",false);
			$("#borrowTypes3").attr("checked",false);
			$("#borrowTypes4").attr("checked",false);
			$("#borrowTypes5").attr("checked",false);
			$("#borrowTypes6").attr("checked",false);
			$("#borrowTypes8").attr("checked",false);
			$("#borrowTypes2").attr("checked",false);
			$("#borrowTypes9").attr("checked",false);
			$("#borrowTypes10").attr("checked",false);
			$("#borrowTypes99").attr("checked",false);
			
			if($("#borrowTypes7").attr("checked")){	
				$("#borrowTypes1").attr("disabled",true);
				$("#borrowTypes3").attr("disabled",true);
				$("#borrowTypes4").attr("disabled",true);
				$("#borrowTypes5").attr("disabled",true);
				$("#borrowTypes6").attr("disabled",true);
				$("#borrowTypes8").attr("disabled",true);
				$("#borrowTypes2").attr("disabled",true);
				$("#borrowTypes9").attr("disabled",true);
				$("#borrowTypes10").attr("disabled",true);
				$("#borrowTypes99").attr("disabled",true);
			}else{
				$("#borrowTypes1").attr("disabled",false);
				$("#borrowTypes3").attr("disabled",false);
				$("#borrowTypes4").attr("disabled",false);
				$("#borrowTypes5").attr("disabled",false);
				$("#borrowTypes6").attr("disabled",false);
				$("#borrowTypes8").attr("disabled",false);
				$("#borrowTypes2").attr("disabled",false);
				$("#borrowTypes9").attr("disabled",false);
				$("#borrowTypes10").attr("disabled",false);
				$("#borrowTypes99").attr("disabled",false);
			}
		}); 
		
		$('#investAmountCheckType').combobox({
			onSelect:function(newValue,oldValue){
				var termVal = $('#investAmountCheckType').combobox('getValue');
				if(termVal == 1){
					$("#_investAmountDivisor").hide();
					$("#investAmountDivisor").numberbox({ required: false });
					$("#_investAmountFrom").show();
					$("#_investAmountTo").show();
				}else{
					$("#_investAmountDivisor").show();
					$("#investAmountDivisor").numberbox({ required: true });
					$("#_investAmountFrom").hide();
					$("#_investAmountTo").hide();
				}
			}
		});
		
		initCorporationCombobox();
	})
		
	
	function addRule(){
		initComboBox();
		
		$('#add_rule_dialog').dialog('open').dialog("setTitle", "添加设置");
		$("#add_rule_form").form('reset');
		$("#_investAmountDivisor").hide();
		$("#investAmountDivisor").numberbox({ required: false });
		$("#_investAmountFrom").show();
		$("#_investAmountTo").show();
		$("#rule_flag").val("add");
	}
	
	function initComboBox(){
		$('#ticketId').combobox({
			url:'/lotteryTicket/ticketCombox',
			valueField:'id',
			textField:'title'
		});
	}
	
	var ruleCount = 0;
	function ruleSave(){
		var validate=$("#add_rule_form").form('validate');
		if(!validate){
			return;
		}
		
		var investMonth = $("#investMonth").val();
		var investAmountFrom = $("#investAmountFrom").val();
		var investAmountTo = $("#investAmountTo").val();
		var ticketId = $('#ticketId').combobox('getValue');
		var ticketText = $('#ticketId').combobox('getText');
		var investAmountDivisor = $("#investAmountDivisor").numberbox('getValue');
		var investAmountCheckType = $('#investAmountCheckType').combobox('getValue');
		var investAmountCheckText = $('#investAmountCheckType').combobox('getText');
		
		var investAmountText="";
		if(1==investAmountCheckType){
			if((""==investAmountFrom || null == investAmountFrom) && (""==investAmountTo || null == investAmountTo)){
				investAmountText = "无限制";
			}else if(""==investAmountFrom || null == investAmountFrom){
				investAmountText = "<="+investAmountTo+"元";
			}else if(""==investAmountTo || null == investAmountTo){
				investAmountText = ">="+investAmountFrom+"元";
			}else{
				investAmountText = investAmountFrom+"<=投资金额<="+investAmountTo;
			}
		}else{
			investAmountText = investAmountDivisor+"元";
		}
		
		
		var ruleFlag=$("#rule_flag").val(); 
		
		if("add" == ruleFlag){
			
			var flag = true;
			$(".rule_success").each(function(i,e){
				if(investAmountDivisor==$(e).find("input[class=investAmountDivisor]").val() && $(e).find("input[class=investAmountDivisor]").val() != null &&
						"" != $(e).find("input[class=investAmountDivisor]").val()){
						flag = false;
					}
			});
			/* if(!flag){
				$.messager.alert('错误',"获取条件重复！");
				return;
				} */
			
			var trId = "rule_"+ruleCount;
			var str = "<tr id='"+trId+"' class='rule_success'>";
			str = str+"<td><input type='hidden' id='rule_"+ruleCount+"_status'  name='ruleList["+ruleCount+"].status' value='1'>";
			str = str+"<input class='investMonth' type='hidden' id='rule_"+ruleCount+"_investMonth'  name='ruleList["+ruleCount+"].investMonth' value='"+investMonth+"'>";
			str = str+"<input class='investAmountCheckType' type='hidden' id='rule_"+ruleCount+"_investAmountCheckType'  name='ruleList["+ruleCount+"].investAmountCheckType' value='"+investAmountCheckType+"'>";
			str = str+"<input class='investAmountDivisor' type='hidden' id='rule_"+ruleCount+"_investAmountDivisor'  name='ruleList["+ruleCount+"].investAmountDivisor' value='"+investAmountDivisor+"'>";
			str = str+"<span id='rule_"+ruleCount+"_names'>"+investMonth+"月</span></td>";
			str = str+"<td><span id='rule_"+ruleCount+"_ct'>"+investAmountCheckText+"</span></td>";
			str = str+"<td><input class='investAmountTo' type='hidden' id='rule_"+ruleCount+"_investAmountTo'  name='ruleList["+ruleCount+"].investAmountTo' value='"+investAmountTo+"'>";
			str = str+"<input class='investAmountFrom' type='hidden' id='rule_"+ruleCount+"_investAmountFrom'  name='ruleList["+ruleCount+"].investAmountFrom' value='"+investAmountFrom+"'>";
			str = str+"<span id='rule_"+ruleCount+"_ps'>"+investAmountText+"</span></td>";
			str = str+"<td><input class='investTicketId' type='hidden' id='rule_"+ruleCount+"_ticketId' name='ruleList["+ruleCount+"].ticketId' value='"+ticketId+"'>";
			str = str+"<span id='rule_"+ruleCount+"_text'>"+ticketText+"</span</td>";
			str = str+"<td><a href='#'onclick='updateRule("+ruleCount+")'>修改</a>|<a href='#' onclick='deleteRule(this)'>删除</a></td>";
			$('#table_rule tbody').append(str);
			ruleCount++;
			$('#add_rule_dialog').dialog('close');
		}
		
		if("edit"==ruleFlag){
			var flag = true;
			var ruleNum=$("#rule_count").val();
			var trId = "rule_"+ruleNum;
			var str = "";
			
			$(".rule_success").each(function(i,e){
				if($(e).attr("id")!=trId){
				if(investAmountDivisor==$(e).find("input[class=investAmountDivisor]").val()&& $(e).find("input[class=investAmountDivisor]").val() != null &&
						"" != $(e).find("input[class=investAmountDivisor]").val()){
						flag = false;
					}
				}
				});
			if(!flag){
				$.messager.alert('错误',"获取条件重复！");
				return;
				}

			$("#rule_"+ruleNum+"_investAmountFrom").val(investAmountFrom);
			$("#rule_"+ruleNum+"_investAmountTo").val(investAmountTo);
			$("#rule_"+ruleNum+"_investMonth").val(investMonth);
			$("#rule_"+ruleNum+"_ticketId").val(ticketId);
			$("#rule_"+ruleNum+"_investAmountCheckType").val(investAmountCheckType);
			$("#rule_"+ruleNum+"_investAmountDivisor").val(investAmountDivisor);
			
			$("#rule_"+ruleNum+"_names").html(investMonth+"月");
			$("#rule_"+ruleNum+"_ps").html(investAmountText);
			$("#rule_"+ruleNum+"_text").html(ticketText);
			$("#rule_"+ruleNum+"_ct").html(investAmountCheckText);
			
			$('#add_rule_dialog').dialog('close');
		}
		
	}
	
	/**
	 * 修改抽奖设置
	 */
	function updateRule(count){
		initComboBox();
		
		var e=count;
		$('#add_rule_dialog').dialog('open').dialog("setTitle", "修改规则设置");
		$("#add_rule_form").form('reset');
	
	    $("#rule_flag").val("edit");
	    $("#rule_count").val(e);

	    var ticketId=$("#rule_"+e+"_ticketId").val();
	    var ticketText=$("#rule_"+e+"_ticketText").val();
	    
	    $('#ticketId').combobox('setValue',ticketId); 
	    $('#ticketId').combobox('setText',ticketText); 
	   

	    var investMonth=$("#rule_"+e+"_investMonth").val();
	    $("#investMonth").numberbox('setValue', investMonth);
	    
	    var investAmountFrom=$("#rule_"+e+"_investAmountFrom").val();
	    $("#investAmountFrom").numberbox('setValue', investAmountFrom);
	    
	    var investAmountTo=$("#rule_"+e+"_investAmountTo").val();
	    $("#investAmountTo").numberbox('setValue', investAmountTo);
	    
	    var investAmountDivisor=$("#rule_"+e+"_investAmountDivisor").val();
	    $("#investAmountDivisor").numberbox('setValue', investAmountDivisor);
	    
	    var investAmountCheckType=$("#rule_"+e+"_investAmountCheckType").val();
	    $('#investAmountCheckType').combobox('setValue',investAmountCheckType); 
	    
	}
	
	//添加规则 取消
	function ruleCancel() {
	    $('#add_rule_dialog').dialog('close');
	}
	
	function deleteRule(obj){
		$.messager.confirm('确认','是否确认删除该规则设置?',function(row){  
	        if(row){  
	        	$(obj).parent().parent().hide();
	        	var id=$(obj).parent().parent().attr('id');
	        	$(obj).parent().parent().removeClass('rule_success');
	        	$(obj).parent().parent().addClass('rule_fail');
	        	$('#'+id+'_status').val(0);
	        }
	    })
	}
	
	
	function publishPlatformsClick(){
		var publishPlatforms = "";
		if($("#publishPlatforms1").attr("checked")){
			publishPlatforms = "1"
		}
		if($("#publishPlatforms2").attr("checked")){
			if(publishPlatforms!=""){
				publishPlatforms = publishPlatforms+",2"
			}else{
				publishPlatforms = "2"
			}
		}
		if($("#publishPlatforms3").attr("checked")){
			if(publishPlatforms!=""){
				publishPlatforms = publishPlatforms+",3"
			}else{
				publishPlatforms = "3"
			}
		}
		$("#publishPlatforms").val(publishPlatforms);
	}
	
	function borrowTypesClick(){
		var borrowTypes = "";
		if($("#borrowTypes1").attr("checked")){
			borrowTypes = "3"
		}
		if($("#borrowTypes2").attr("checked")){
			if(borrowTypes!=""){
				borrowTypes = borrowTypes+",1"
			}else{
				borrowTypes = "1"
			}
		}
		if($("#borrowTypes3").attr("checked")){
			if(borrowTypes!=""){
				borrowTypes = borrowTypes+",2"
			}else{
				borrowTypes = "2"
			}
		}
		if($("#borrowTypes4").attr("checked")){
			if(borrowTypes!=""){
				borrowTypes = borrowTypes+",0"
			}else{
				borrowTypes = "0"
			}
		}
		if($("#borrowTypes5").attr("checked")){
			if(borrowTypes!=""){
				borrowTypes = borrowTypes+",5"
			}else{
				borrowTypes = "5"
			}
		}
		if($("#borrowTypes6").attr("checked")){
			if(borrowTypes!=""){
				borrowTypes = borrowTypes+",6"
			}else{
				borrowTypes = "6"
			}
		}
		if($("#borrowTypes8").attr("checked")){
			if(borrowTypes!=""){
				borrowTypes = borrowTypes+",8"
			}else{
				borrowTypes = "8"
			}
		}
		if($("#borrowTypes9").attr("checked")){
			if(borrowTypes!=""){
				borrowTypes = borrowTypes+",9"
			}else{
				borrowTypes = "9"
			}
		}
		if($("#borrowTypes10").attr("checked")){
			if(borrowTypes!=""){
				borrowTypes = borrowTypes+",10"
			}else{
				borrowTypes = "10"
			}
		}
		if($("#borrowTypes99").attr("checked")){
			if(borrowTypes!=""){
				borrowTypes = borrowTypes+",99"
			}else{
				borrowTypes = "99"
			}
		}
		$("#borrowTypes").val(borrowTypes);
	}
	
	function submit(){
		publishPlatformsClick();
    	borrowTypesClick();
    	getAllCoporationIds(); 
    	$.messager.progress({text:'执行中...'});
    	
    	$('#addInvestActivityForm').form('submit',{
            url: '/activity/addNewInvestActivity',
            onSubmit: function(){
            	var validate = $(this).form('validate');
            	if(!validate){
            		$.messager.progress('close'); 
            	}
                return validate;
            },
            success: function(result){
            	$.messager.progress('close'); 
            	var result = eval('('+result+')');
            	if (result.code==-1) {
    				$.messager.alert('错误',result.msg);
    			} else {
    				$.messager.alert("操作提示", "投资活动添加成功！", "info", function () {  
    					window.parent.closeAddWin();      
    		        });
    			}
            }
        });
	}
	
	function searchActTitle(){
 		var userName = $("#title").val();
 		if(userName != ""){
  	    $.post('<%=basePath%>/activity/checkActivityTitle',{title:$("#title").val()},function(result){
  	    	if (result.code == -1 ){
            	   $.messager.alert('提示',result.msg,'info',function(){
						$("#userName").val("");
            	   });
            }
         },'json');
 		
 		}
    }
	
	var allCoporationObj;
	function initCorporationCombobox(){
		$.ajax({
            type: 'GET',
            url: '<%=basePath%>/activity/queryAllcorpor',
            dataType: "json",
            cache: false,
            async: false,
            success: function(data){
            	allCoporationObj = data;//将所有合作机构保存为全局变量
            	firstRow();
            }
        });
    }
	//给前置条件赋值，即，将机构ID以逗号分隔后保存在activitiy表中的rule字段中
	function getAllCoporationIds(){
		var coporationIds = "";
		var coporationIdArr = document.getElementsByName("cooperNames");
		for(var i=0;i<coporationIdArr.length;i++){
			var coporation = coporationIdArr[i].value;
			if(coporation){
				coporationIds+=coporation+",";
			}
		}
		$('#rewardPreRule').val(coporationIds);
	}
	function firstRow(){
		var tb = document.getElementById("optionContainer");
        var row = tb.insertRow();
        var cell = row.insertCell();
        var options = '<select name = "cooperNames" onchange="checkRow(this)"><option value="">-无限制-</option>';
        for(var i=0;i < allCoporationObj.length;i++){
        	options += ('<option value="'+allCoporationObj[i].code+'">'+allCoporationObj[i].title+'</option>');
        }
        options += '</select>'
        //alert(options);
        cell.innerHTML = options;
        cell = row.insertCell();
        cell.innerHTML = '<a href="#" onclick="addRow()">添加</a>';
    }
	//添加行 
	function addRow(){
		var tb = document.getElementById("optionContainer");
        var row = tb.insertRow();
        var cell = row.insertCell();
        var options = '<select name = "cooperNames" onchange="checkRow(this)"><option value="">-无限制-</option>';
        for(var i=0;i<allCoporationObj.length;i++){
        	options += ('<option value="'+allCoporationObj[i].code+'">'+allCoporationObj[i].title+'</option>');
        }
        options += '</select>'
        //alert(options);
        cell.innerHTML = options;
        cell = row.insertCell();
        cell.innerHTML = '<a href="#" onclick="delRow(this)">删除</a>';
    }
	//删除行  
	function delRow(elem) {
		//alert(elem);
		var tb = document.getElementById("optionContainer");
		var trEle = elem.parentNode.parentNode;
		var trIndex = trEle.rowIndex;
     //alert(trEle.rowIndex);
		tb.deleteRow(trIndex)
	}
	//检测所选机构是否有重复
	function checkRow(elem){
		var elemVal = elem.value;
		var allCooperStr = "";
		var allCooperEle = document.getElementsByName("cooperNames");
		for(var i=0;i<allCooperEle.length;i++){
			var tmp = allCooperEle[i].value;
			if(tmp){
			allCooperStr += (tmp+",");
			}
     }
		if(elemVal){
			var reg = new RegExp(elemVal+",","g");
			if(allCooperStr.match(reg).length>=2){
				alert("请不要选择重复的机构！");
				elem.options[0].selected = true;
			}
		}
	}
	</script>
</body>
</html>